# -*- coding: utf-8 -*-
import time
from tool import plog, json_post, host_db, get_log
from cfg import g_tech, g_line, g_station
from esop import get_esop
from bom import get_bom


def do_end(pre_que):
    log = get_log("op")

    while True:
        try:
            info = pre_que.get()

            log.info(info)

            pre_que.task_done()
        except Exception as e:
            print("do_end error: %s" % e)


def add_station():
    for n in range(g_station):
        sn = "M%s" % n

        s = time.time()

        json_post(host_db("station/add"), {
            "line": g_line,
            "v": {
                "sn": sn,
                "name": "工位号这是一个工位工位_%s" % sn
                }
            })

        plog("addstation", s, n, sn)


def add_rule():
    for n in range(g_tech):
        sn = "te%s" % n
        rule = "code%s" % n
        s = time.time()

        json_post(host_db("tech/md"), {
            "line": g_line,
            "k": sn,
            "v": {
                "code": {
                    "M1": {
                        "rule": rule,
                        "ext": [
                            {"sn": "c1", "rule": rule},
                            {"sn": "c2", "rule": rule},
                            ]
                        }
                    }
                }
            })

        plog("addrule", s, n, sn)


def add_bom():
    for n in range(g_tech):
        sn = "te%s" % n

        for m in range(g_station / 50):
            v = get_bom(m, 50)
            s = time.time()

            json_post(host_db("bom/add"), {
                "line": g_line,
                "k": sn,
                "v": v,
                })

            plog("addbom", s, n, sn)


def add_code():
    for n in range(g_tech):
        sn = "code%s" % n

        s = time.time()

        json_post(host_db("code/add"), {
            "line": g_line,
            "v": {
                "sn": sn,
                "name": "工位号这是一个工位工位_%s" % sn,
                "v": [
                    {"type": "fix", "name": "fix", "keyv": "AAA", "num": 3},
                    {"type": "task", "name": "task", "keyv": "code", "num": 4},
                    {"type": "date", "name": "gb34014", "keyv": "gb34014"},
                    {"type": "date", "name": "gb16735", "keyv": "gb16735"},
                    {"type": "date", "name": "kt", "keyv": "kt"},
                    {"type": "date", "name": "YYM", "keyv": "YYM"},
                    {"type": "num", "name": "num", "keym": "range,val,max", "keyv": "day,1,5", "num": 3},
                    {"type": "gnum1", "name": "gnum1", "keym": "range,val,max", "keyv": "day,1,5", "num": 3},
                    {"type": "num32", "name": "num32", "keym": "range,val", "keyv": "day,1", "num": 3},
                    ]
                }
            })

        plog("addesop", s, n, sn)


def add_esop():
    for n in range(g_tech):
        sn = "te%s" % n

        for m in range(g_station / 50):
            v = get_esop(m, 50)
            s = time.time()

            json_post(host_db("esop/add"), {
                "line": g_line,
                "k": sn,
                "v": v,
                })

            plog("addesop", s, n, sn)


def add_task():
    for n in range(g_tech):
        sn = "task%s" % n

        s = time.time()

        json_post(host_db("task/add"), {
            "line": g_line,
            "v": {
                "sn": sn,
                "tech": "te%s" % n,
                "pstart": "2024-01-01 01:01:01",
                "pend": "2024-02-01 01:01:01",
                "pnum": 1000,
                "state": "open",
                "code": "TTTT",
                }
            })

        plog("addtask", s, n, sn)


def add_tech():
    for n in range(g_tech):
        sn = "te%s" % n

        s = time.time()

        json_post(host_db("tech/add"), {
            "line": g_line,
            "v": {
                "sn": sn,
                "name": "工位号这是一个工位工位_%s" % sn
                }
            })

        plog("addtech", s, n, sn)


def get_psn(wid):
    s = time.time()

    ret = json_post(host_db("code/getpsn"), {
        "line": "1",
        "wsn": "M1",
        })

    plog("getpsn", s, wid, "M1")

    return ret


def do_client(wid):
    add_station()
    # add_tech()
    # add_task()
    # add_code()

    # add_esop()
    # add_bom()
    # add_rule()

    # while True:
    #   ret = get_psn(wid)
    #   if ret is None:
    #       break
